/**

 @Name：layuiAdmin 用户管理 管理员管理 角色管理
 @Author：star1029
 @Site：http://www.layui.com/admin/
 @License：LPPL
    
 */


layui.define(['table', 'form'], function(exports){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,form = layui.form;
  form.render(null, 'layadmin-yearsfront-formlist');

  //监听搜索
  form.on('submit(LAY-years-front-search)', function(data){
    var field = data.field;

    //执行重载
    table.reload('LAY-years-manage', {
      page: {
        curr: 1
      }
      ,where: field
      ,method: 'post'
    });
  });

  //用户管理
  table.render({
    elem: '#LAY-years-manage'
    ,url: '/getAllYearsServlet' //模拟接口
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'yearsId', width: 100, title: 'ID', sort: true, hide: true}
      ,{field: 'yearsname', title: '年份', minWidth: 100, sort: true}
      ,{field: 'yearsflag', width: 80, title: '状态'}
      ,{title: '操作', width: 150, align:'center', fixed: 'right', toolbar: '#table-yearsadmin-webyears'}
    ]]
    ,page: true
    ,limit: 10
    ,height: 'full-320'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听工具条
  table.on('tool(LAY-years-manage)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){//删除
      layer.prompt({
        formType: 1
        ,title: '敏感操作，请验证口令'
      }, function(value, index){
        layer.close(index);
        
        layer.confirm('真的删除行么', function(index){
          $.ajax({
              type: 'POST',
              url: '/C3P0Project/deleteYearsServlet',
              data: {yearsId: data.yearsId},
              success: function(data){
                if(data === "Ok"){
                  layer.alert("删除成功",  {icon: 6});
                }else {
                  layer.alert(data,{title:"删除失败"})
                }
              }
          });
          obj.del();
          layer.close(index);
        });
      });
    } else if(obj.event === 'edit'){//编辑
      admin.popup({
        title: '编辑年份'
        ,area: ['500px', '450px']
        ,id: 'LAY-popup-years-add'
        ,success: function(layero, index){
          view(this.id).render('years/update', data).done(function(){
            form.render(null, 'layuiadmin-form-yearsadmin');
            
            //监听提交
            form.on('submit(LAY-years-front-submit)', function(data){
              var field = data.field; //获取提交的字段

              //提交 Ajax 成功后，关闭当前弹层并重载表格
              $.ajax({
                type: 'POST',
                url: '/C3P0Project/updateYearsServlet',
                data: field,
                success: function(data){
                  if(data === "Ok"){
                    layer.alert("修改成功",  {icon: 6});
                  }else {
                    layer.alert(data,{title:"修改失败"})
                  }
                }
              });
              layui.table.reload('LAY-years-manage'); //重载表格
              layer.close(index); //执行关闭 
            });
          });
        }
      });
    }
  });

  exports('yearsadmin', {});

  //事件
  var active = {
    batchdel: function(){
      var checkStatus = table.checkStatus('LAY-years-manage')
          ,checkData = checkStatus.data; //得到选中的数据

      if(checkData.length === 0){
        return layer.msg('请选择数据');
      }

      layer.prompt({
        formType: 1
        ,title: '敏感操作，请验证口令'
      }, function(value, index){
        layer.close(index);

        layer.confirm('确定删除吗？', function(index) {

          //执行 Ajax 后重载
          /*
          admin.req({
            url: 'xxx'
            //,……
          });
          */
          table.reload('LAY-years-manage');
          layer.msg('已删除');
        });
      });
    }
    ,add: function(){
      admin.popup({
        title: '添加年份'
        ,area: ['500px', '300px']
        ,id: 'LAY-popup-years-add'
        ,success: function(layero, index){
          view(this.id).render('years/add').done(function(){
            form.render(null, 'layuiadmin-form-yearsadmin');

            //监听提交
            form.on('submit(LAY-years-front-submit)', function(data){//此处data为前端页面字段
              var field = data.field; //获取提交的字段

              //提交 Ajax 成功后，关闭当前弹层并重载表格
              $.ajax({
                type: 'POST',
                url: "/C3P0Project/addYearsServlet",
                data: field,
                success: function(data){//此处data为后端返回的数据
                  if (data === "Ok"){
                    layer.alert("添加成功",  {icon: 6});
                  }else{
                    layer.alert(data, {title: '添加失败'});
                  }
                }
              });
              layui.table.reload('LAY-years-manage'); //重载表格
              layer.close(index); //执行关闭
            });
          });
        }
      });
    }
  };

  $('.layui-btn.layuiadmin-btn-yearsadmin').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});